package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.comment;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommentResult;

import java.util.List;

public class CommentNormalAdapter extends BaseQuickAdapter<CommentResult, BaseViewHolder> {

    private final int MAX_COUNT = 2;

    public CommentNormalAdapter(@Nullable List<CommentResult> data) {
        super(R.layout.item_comments_short, data);
    }

    public void setList(@Nullable List<CommentResult> list) {
        assert list != null;
        if (list.size() > MAX_COUNT) {
            list = list.subList(0, MAX_COUNT - 1);
        }
        super.setList(list);
    }

    public void addNewComment(CommentResult result) {
        if (getData().size() == MAX_COUNT) {
            removeAt(MAX_COUNT - 1);
        }
        addData(0, result);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentResult item) {
        TextView tvComment = helper.getView(R.id.tv_comment);
        //tvComment.setText(item.getNickname());
        int count = item.getNickname() == null ? 0 : item.getNickname().length();
        SpannableString ss = new SpannableString(item.getNickname() + ":  " + item.getCommentContent());
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#333333"));
        ss.setSpan(colorSpan, 0, count + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvComment.setText(ss);
    }

}
